#ifndef __JPEG_DECODER_H__
#define __JPEG_DECODER_H__

#include "io/iostub.h"
#include "./basedecoder.h"

class JpegDecoder : public BaseDecoder
{
	struct SourceMgr : public jpeg_source_mgr
	{
		byte* buffer;
		size_t bufSize;

		byte* cur;
	};
public:
	JpegDecoder(byte* buffer, size_t size);
	~JpegDecoder();

	ImageData GetImageData();

private:
	bool CheckFormat(const byte * buffer, size_t size);
	bool StartDecode();
	void ReadImageData();

private:
	static void JpegInitSource(j_decompress_ptr cinfo);
	static boolean JpegFillInputBuffer(j_decompress_ptr cinfo);
	static void JpegSkipInputData(j_decompress_ptr cinfo, long num_bytes);
	static void JpegTermSource(j_decompress_ptr cinfo);

private:
	SourceMgr m_srcmgr;
	ImageData m_jdata;

	jpeg_error_mgr m_jerr;
	jpeg_decompress_struct m_cinfo;
};

#endif